
/*
 * Copyright (C) ZHANG Xin
 */


#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

typedef struct
{
    ngx_str_t author_name;
    ngx_str_t author_email;
} ngx_http_author_loc_conf_t;

static ngx_int_t ngx_http_author_init(ngx_conf_t *cf);
static void *ngx_http_author_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_author_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_author_email(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_author_handler(ngx_http_request_t *r);

static ngx_command_t ngx_http_author_commands[] = {
   {
        ngx_string("author_name"),
        NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
        ngx_http_author_name,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_author_loc_conf_t, author_name),
        NULL },

    {
        ngx_string("author_email"),
        NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
        ngx_http_author_email,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_author_loc_conf_t, author_email),
        NULL },

    ngx_null_command
};

static ngx_http_module_t ngx_http_author_module_ctx = {
    NULL,                           /* preconfiguration */
    ngx_http_author_init,           /* postconfiguration */

    NULL,                          /* create main configuration */
    NULL,                          /* init main configuration */

    NULL,                          /* create server configuration */
    NULL,                          /* merge server configuration */

    ngx_http_author_create_loc_conf, /* create location configuration */
    NULL                             /* merge location configuration */
};

static void *ngx_http_author_create_loc_conf(ngx_conf_t *cf)
{
        ngx_http_author_loc_conf_t* local_conf = NULL;
        local_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_author_loc_conf_t));
        if (local_conf == NULL)
        {
                return NULL;
        }

        ngx_str_null(&local_conf->author_name);
        ngx_str_null(&local_conf->author_email);

        return local_conf;

}

static ngx_int_t
ngx_http_author_init(ngx_conf_t *cf)
{
        ngx_http_handler_pt        *h;
        ngx_http_core_main_conf_t  *cmcf;

        cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

        h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers);
        if (h == NULL) {
                return NGX_ERROR;
        }

        *h = ngx_http_author_handler;

        return NGX_OK;
};

ngx_module_t ngx_http_author_module = {
        NGX_MODULE_V1,
        &ngx_http_author_module_ctx,    /* module context */
        ngx_http_author_commands,       /* module directives */
        NGX_HTTP_MODULE,               /* module type */
        NULL,                          /* init master */
        NULL,                          /* init module */
        NULL,                          /* init process */
        NULL,                          /* init thread */
        NULL,                          /* exit thread */
        NULL,                          /* exit process */
        NULL,                          /* exit master */
        NGX_MODULE_V1_PADDING
};

static char *
ngx_http_author_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{

        ngx_http_author_loc_conf_t* local_conf;


        local_conf = conf;
        char* rv = ngx_conf_set_str_slot(cf, cmd, local_conf);

        return rv;
}

static char *
ngx_http_author_email(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{

        ngx_http_author_loc_conf_t* local_conf = conf;
        char* rv = ngx_conf_set_str_slot(cf, cmd, local_conf);

        return rv;
}

static ngx_int_t 
ngx_http_author_handler(ngx_http_request_t *r) 
{
    ngx_int_t    rc;
    ngx_buf_t   *b;
    ngx_chain_t  out;
    ngx_http_author_loc_conf_t* my_conf;
    u_char ngx_author_json[512] = {0};
    ngx_uint_t content_length = 0;

    my_conf = ngx_http_get_module_loc_conf(r, ngx_http_author_module);
    if (my_conf->author_name.len == 0 )
    {
            ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "author_name is empty!");
            return NGX_DECLINED;
    }

    if (my_conf->author_email.len == 0 )
    {
            ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "author_email is empty!");
            return NGX_DECLINED;
    }

    ngx_sprintf(
        ngx_author_json, 
        "{\"name\":\"%s\",\"email\":\"%s\"}", 
        my_conf->author_name.data, 
        my_conf->author_email.data
    );


    ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "should return JSON:%s", ngx_author_json);
    content_length = ngx_strlen(ngx_author_json);

    /* we response to 'GET' requests only */
    if (!(r->method & (NGX_HTTP_GET))) {
            return NGX_HTTP_NOT_ALLOWED;
    }

    /* discard request body, since we don't need it here */
    rc = ngx_http_discard_request_body(r);

    if (rc != NGX_OK) {
            return rc;
    }

    ngx_str_set(&r->headers_out.content_type, "application/json");


    /* allocate a buffer for your response body */
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if (b == NULL) {
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    /* attach this buffer to the buffer chain */
    out.buf = b;
    out.next = NULL;

    /* adjust the pointers of the buffer */
    b->pos = ngx_author_json;
    b->last = ngx_author_json + content_length;
    b->memory = 1;    /* this buffer is in memory */
    b->last_buf = 1;  /* this is the last buffer in the buffer chain */

    /* set the status line */
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = content_length;

    /* send the headers of your response */
    rc = ngx_http_send_header(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
            return rc;
    }

    /* send the buffer chain of your response */
    return ngx_http_output_filter(r, &out);
}

